package top.infopub.mgr.mkt.service.impl;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.infopub.dto.mkt.TenderPlanningDto;
import top.infopub.enums.mkt.ProjectFileTypeEnum;
import top.infopub.enums.mkt.ProjectFormatEnum;
import top.infopub.mgr.flow.web.GenericFlowController;
import top.infopub.mgr.mkt.dao.ProjectAttachmentDao;
import top.infopub.mgr.mkt.dao.ProjectSellDtoDao;
import top.infopub.mgr.mkt.dao.TenderPlanningDtoDao;
import top.infopub.mgr.mkt.service.TenderPlanningService;
import top.infopub.mgr.util.DateUtils;
import top.infopub.model.mkt.*;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author Warking
 * @Date 2021/5/2 13:55
 * @Version 1.0
 */
@Service
public class TenderPlanningServiceImpl extends GenericFlowController implements TenderPlanningService {

    @Autowired
    private ProjectSellDtoDao projectSellDtoDao;

    @Autowired
    private ProjectAttachmentDao projectAttachmentDao;
    @Autowired
    private CollectProjectServiceImpl collectProjectService;

    @Autowired
    private TenderPlanningDtoDao tenderPlanningDtoDao;


    @Override
    public TenderPlanningDto queryDetail(String code) {

        TenderPlanningDto tenderPlanningDto = new TenderPlanningDto();
        //查询基本信息
        ProjectInfo projectInfo = projectSellDtoDao.findByProCode(code);//id是code
        //项目业态-文字映射
        ProjectFormatEnum pfe = ProjectFormatEnum.getByCode(projectInfo.getProFormat());
        if (pfe != null) {
            projectInfo.setProFormat(pfe.getName());
        }
        //基本信息
        tenderPlanningDto.setProjectInfo(projectInfo);
        //查询投标信息
        BidInfo bidInfo = tenderPlanningDtoDao.biddingInformation(code);
        tenderPlanningDto.setBidInfo(bidInfo);

        //根据proCode查询出是否有资信和技术标
        BidAnalysis bidAnalysis = tenderPlanningDtoDao.analysisDetail(code);
        //查询资信标的人员ID和配合的IDS
        if ("1".equals(bidAnalysis.getIsCredit()) && bidAnalysis != null) {
            CreditDetail creditDetail = new CreditDetail();
            CreditDetail  creditDetail1  = tenderPlanningDtoDao.creditDetail(code);
            if (creditDetail1 != null) {
               JSON.toJSONString(creditDetail1.getCommerceCooperateSguids());
                tenderPlanningDto.setCreditDetail(creditDetail1);
            }else {
                creditDetail.setWhether("1");
                tenderPlanningDto.setCreditDetail(creditDetail);
            }

        }
        //查询技术标的人员ID和配合的IDS
        if ("1".equals(bidAnalysis.getIsTechnology()) && bidAnalysis != null) {
            TechnicalDetail technicalDetail = new TechnicalDetail();
            TechnicalDetail  technicalDetail1  = tenderPlanningDtoDao.technicalDetail(code);
            if (technicalDetail1 != null) {
                JSON.toJSONString(technicalDetail1.getCommerceCooperateSguids());
                tenderPlanningDto.setTechnicalDetail(technicalDetail1);
            }
            else {
                technicalDetail.setWhether("1");
                tenderPlanningDto.setTechnicalDetail(technicalDetail);
            }

        }
        //查询商务标的人员ID和配合的IDS
        BusinessDetails businessDetails = new BusinessDetails();
        BusinessDetails   businessDetails1  = tenderPlanningDtoDao.standDetail(code);
        if (businessDetails1 != null) {
            JSON.toJSONString(businessDetails1.getCommerceCooperateSguids());
            tenderPlanningDto.setBusinessDetails(businessDetails1);
        }else {
            businessDetails.setWhether("1");
            tenderPlanningDto.setBusinessDetails(businessDetails);
        }
        //附件信息
        //附件查询
        //附件信息表
        List<ProjectAttachment> projectAttaList = projectAttachmentDao.queryByWhere(code, ProjectFileTypeEnum.FILE_TYPE15.getCode(), null);
        tenderPlanningDto.setFileAtt(collectProjectService.fileAttaListToArray(projectAttaList));

        return tenderPlanningDto;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int save(TenderPlanningDto tenderPlanningDto, String code) {
        BidInfo bidInfo = tenderPlanningDto.getBidInfo();
        if (bidInfo != null) {
            bidInfo.setProCode(code);

            tenderPlanningDtoDao.saveBidInfo(bidInfo);
            if (bidInfo.getBidHead() != null && bidInfo.getBidHead() != "") {
                tenderPlanningDtoDao.saveBidPlan(bidInfo);
            }
        }
        //获取用户
        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
        //修改各个标 商业 标书 技术
        //根据proCode查询出是否有资信和技术标
        BidAnalysis bidAnalysis = tenderPlanningDtoDao.analysisDetail(code);
        //查询技术标的人员ID和配合的IDS
        if ("1".equals(bidAnalysis.getIsTechnology()) && bidAnalysis != null) {

            TechnicalDetail detail = tenderPlanningDtoDao.technicalDetail(code);
            TechnicalDetail technicalDetail = tenderPlanningDto.getTechnicalDetail();
            technicalDetail.setUptBy(vo.getId());//z最后修改人
            technicalDetail.setUptTime(DateUtils.getCurrentDateTime());//z最后修改时间
            if (detail == null) {
                technicalDetail.setAddBy(vo.getId());//添加修改人
                technicalDetail.setProCode(code);
                technicalDetail.setAddTime(DateUtils.getCurrentDateTime());//添加修改时间
                tenderPlanningDtoDao.insertTechnicalDetail2(technicalDetail);
            } else {
                if (technicalDetail.getCommercePeopleSguid() != null && technicalDetail.getCommercePeopleSguid() != "") {
                    tenderPlanningDtoDao.updateTechnical(technicalDetail);
                }
            }
        }
        BusinessDetails businessDetails1 = tenderPlanningDtoDao.standDetail(code);
        BusinessDetails businessDetails2 = tenderPlanningDto.getBusinessDetails();
        businessDetails2.setUptBy(vo.getId());//z最后修改人
        businessDetails2.setUptTime(DateUtils.getCurrentDateTime());//z最后修改时间
        if (businessDetails1 == null)
        {
            businessDetails2.setAddBy(vo.getId());//z最后修改人
            businessDetails2.setProCode(code);
            businessDetails2.setAddTime(DateUtils.getCurrentDateTime());//z最后修改时间
            tenderPlanningDtoDao.insertBusinessDetail2(businessDetails2);
        }else {
            if (businessDetails2.getCommercePeopleSguid() != null && businessDetails1.getCommercePeopleSguid() != "") {
                tenderPlanningDtoDao.updateBusiness(businessDetails2);
            }
        }

        //查询资信标的人员ID和配合的IDS
        if ("1".equals(bidAnalysis.getIsCredit()) && bidAnalysis != null) {
            CreditDetail creditDetail1 = tenderPlanningDtoDao.creditDetail(code);
            CreditDetail creditDetail = tenderPlanningDto.getCreditDetail();
            creditDetail.setUptBy(vo.getId());//z最后修改人
            creditDetail.setUptTime(DateUtils.getCurrentDateTime());//z最后修改时间
            if (creditDetail1 == null) {
                creditDetail.setAddBy(vo.getId());//添加修改人
                creditDetail.setProCode(code);
                creditDetail.setAddTime(DateUtils.getCurrentDateTime());//添加修改时间
                tenderPlanningDtoDao.insertCreditDetail2(creditDetail);
            } else {
                if (creditDetail.getCommercePeopleSguid() != null && creditDetail.getCommercePeopleSguid() != "") {
                    tenderPlanningDtoDao.updateCredit(creditDetail);
                }
            }

        }

        //前端附件信息
        //更新附件表-MKT_PROJECT_ATTACHMENT
        List<ProjectAttachment> attachmentList = new ArrayList<>();
        if (tenderPlanningDto.getFileAtt() != null) {
            String[] fileAtt = tenderPlanningDto.getFileAtt();
            for (String file : fileAtt) {
                ProjectAttachment attachment = createProjectFileAtta(vo, code, file);
                attachmentList.add(attachment);
            }
        }
        //更新附件信息
        projectAttachmentDao.deleteByWhere(code, ProjectFileTypeEnum.FILE_TYPE15.getCode(), null);
        if (attachmentList.size() > 0 && attachmentList != null) {
            projectAttachmentDao.insertBatch(attachmentList);
        }
        return 1;
    }

    @Override
    public String isFlag(String proCode) {
        return tenderPlanningDtoDao.isFlag(proCode);
    }

    @Override
    public BidPlan selectByProCode(String proCode) {
        return tenderPlanningDtoDao.selectByProCode(proCode);
    }
}
